package core
{
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.net.URLRequest;
	import flash.system.Capabilities;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;

	public class Main extends Sprite
	{
		public function Main()
		{
		}
		
		public function connect(im:*):void
		{
			try {
				im.stage.nativeWindow;
			}
			catch (error:*) {
				im.Swf.remove(im.Swf.hasSwf("swf", parent));
				return;
			}
			TEvent.newTrigger(Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream().NAME, __triggerHandler);
			im = null;
		}
		
		private function __triggerHandler(type:String, data:* = null):void
		{
			switch (type) {
				case "ON_METADATA":
					Ais.IMain.stage.nativeWindow.visible = false;
					var _scale:Number = this.loaderInfo.parameters["scale"];
					_scale = _scale ? _scale : 1;
					var _state:String = Ais.IMain.stage.displayState;
					var _w:Number = parseInt(data.width) * _scale;
					var _h:Number = parseInt(data.height) * _scale;
					Ais.IMain.stage.nativeWindow.width = _w;
					Ais.IMain.stage.nativeWindow.height = _h;
					if (parseInt(this.loaderInfo.parameters["center"]) !== 0) {
						Ais.IMain.stage.nativeWindow.x = (Capabilities.screenResolutionX - _w) * 0.5;
						Ais.IMain.stage.nativeWindow.y = (Capabilities.screenResolutionY - _h) * 0.5;
					}
					if (_state !== StageDisplayState["NORMAL"]) Ais.IMain.stage.displayState = _state;
					Ais.IMain.stage.nativeWindow.activate();
					if (Ais.IMain.Swf.hasKey("iplayer_1") !== -1) {
						var d:Object = Ais.IMain.Swf.getClass("iplayer_1", "core.model.PSharedObject").getInstance().data;
						Ais.IMain.stage.nativeWindow.title = d.p_list[d.p_int][1];
						d = null;
					}
					_state = null;
					break;
				case "CHANGE_PLAY_NAME":
					if (Ais.IMain.Swf.hasKey("iplayer_1") !== -1) {
						var p_list:Array = Ais.IMain.Swf.getClass("iplayer_1", "core.model.PSharedObject").getInstance().data.p_list;
						for each (var i:Array in p_list) {
							var r:URLRequest = Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream().getURLRequest();
							if (null !== r && r.url === i[0]) {
								Ais.IMain.stage.nativeWindow.title = i[1];
								break;
							}
							r = null;
						}
						p_list = null;
					}
					break;
			}
			type = null;
			data = null;
		}
		
		public function clear():void
		{
			TEvent.removeTrigger(Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream().NAME, __triggerHandler);
		}
		
	}
}